import axios from 'axios'
import {nanoid} from 'nanoid';


export default {

    namespaced: true,
    actions: {
        addPersonFromServer(context){
            axios.get('http://localhost:8080/hitokoto/api.php').then(
                response=>{
                    context.commit('ADD_PERSON',{id:nanoid(),name:response.data})
                },
                error=>{
                    alert(error.message)
                }
            )
        }
    },
    mutations: {
        ADD_PERSON(state, value) {
            console.info('mutations对象中ADD_PERSON被调用了...')
            state.personList.unshift(value)
        }
    },
    state: {

        personList: [
            { id: 1, name: '李四' }
        ]
    },
    getters: {
        getFirstPersonName(state){
            return state.personList[0].name
        }
    }

}